﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraReports.UI;

using DrugsShopManagement.DBCLASS;

namespace DrugsShopManagement
{
    public partial class FrmDrugsOut : DevExpress.XtraEditors.XtraForm
    {
        public FrmDrugsOut(string user_no ,string user_name)
        {
            InitializeComponent();
            u_name = user_name;
            u_no = user_no;
           
        }
        Operator op = new Operator();
        //private FrmDrugsOutDetail FrmDrugsOutDetail;
        public string u_no,u_name;

        public void jgtx()//判断出库后剩余库存是否低于报警值下限
        {

            int sq_ccid = Convert.ToInt32(txtBoxYPID.Text);//该出库药品ID
            int sq_sycc = Convert.ToInt32(txtBoxSYKC.Text);//该出库药品剩余库存


            DataTable dt_b = new DataTable();
            string sq_wid = "Select drugsMin from drugsWarning where drugsId='" + sq_ccid.ToString() + "'";
            dt_b = op.getDataTable(sq_wid, "drugsWarning ");

            int strmin = Convert.ToInt32(dt_b.Rows[0][0]);
            if (sq_sycc < strmin)
            {
                labelTX1.Text = "您刚出库的药品【" + txtBoxDrugsName .Text+ "】现已低于库存下限，请联系采购员采购！";
                labelTX1.Visible = true;
            }


        }

        private void FrmDrugsOut_Load(object sender, EventArgs e)
        {
            txtBoxKPYID.Text = u_no;
            txtBoxKPY.Text = u_name;
            dataGridView1.Visible = false;
            dataGridView2.Visible = true;
            buttonBC.Enabled = false;
            buttonXG.Enabled = false;
            buttonQDCK.Enabled = false;
            buttBBDY.Enabled = false;

            this.timer1.Interval = 100;
            this.timer1.Enabled = true;

            DataTable dt = new DataTable();
            string sql_a = "select drugsId 药品ID,drugsName 药品名称, drugsPY 拼音码, drugsSpecification 药品规格,drugsUnit 计量单位, drugsCount 库存数量,drugsStorePlace 库存地址,drugsRegNum 批准文号 ,drugsFactory 生产厂家,drugsUsefulDate 有效期,drugsInPrice 进货价格,drugsSalePrice 销售价格 from drugsStore order by Id";
            dt = op.getDataTable(sql_a, "drugsStore");
            dataGridView2.DataSource = dt;
        }

 

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            buttBBDY.Enabled = true;
            try
            {
                DataTable dt_out = new DataTable();
                dt_out = op.getDataTable("select * from drugsOut where drugsOutIdnum='" + dataGridView1.CurrentRow.Cells[0].Value.ToString() + "'", "drugsOut");

                //this.textBoxCKID.Text = dt_out.Rows[0]["drugsOutId"].ToString();
                this.txtBoxDrugsName.Text = dt_out.Rows[0]["drugsName"].ToString();
                this.txtBoxPY.Text = dt_out.Rows[0]["drugsPY"].ToString();
                this.txtBoxGG.Text = dt_out.Rows[0]["drugsSpecification"].ToString();
                this.txtBoxPH.Text = dt_out.Rows[0]["drugsBatchNo"].ToString();
                this.txtBoxCJ.Text = dt_out.Rows[0]["drugsFactory"].ToString();
                this.txtBoxYXQ.Text = dt_out.Rows[0]["drugsUsefulDate"].ToString();
                this.txtBoxKF.Text = dt_out.Rows[0]["drugsStorePlace"].ToString();
                this.txtBoxUnit.Text = dt_out.Rows[0]["drugsUnit"].ToString();
                this.txtBoxKCSL.Text = dt_out.Rows[0]["drugsCount"].ToString();
                this.txtBoxJHDJ.Text = dt_out.Rows[0]["drugsInPrice"].ToString();
                this.txtBoxXSDJ.Text = dt_out.Rows[0]["drugsSalePrice"].ToString();
                this.txtBoxCKSL.Text = dt_out.Rows[0]["drugsOutNo"].ToString();
                this.txtBoxCKJE.Text = dt_out.Rows[0]["drugsOutMoney"].ToString();
                this.txtBoxYPID.Text = dt_out.Rows[0]["drugsID"].ToString();
                this.textBox1.Text = dt_out.Rows[0]["providerName"].ToString();

                int x1, x2, x3;
                x1 = Convert.ToInt32(txtBoxKCSL.Text);
                x2 = Convert.ToInt32(txtBoxCKSL.Text);
                x3 = x1 - x2;
                txtBoxSYKC.Text = Convert.ToString(x3);

                
              
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); return; }
        }




  

        private void buttonBC_Click(object sender, EventArgs e)
        {

            labelTX1.Visible = false;
            try
            {
                int drugsInNum = Convert.ToInt32(txtBoxSetCKSL.Text);
            }
            catch (Exception)
            {
                MessageBox.Show("输入采购数量格式不正确！");
                txtBoxSetCKSL.Text = "";
                return;
            }

            txtBoxSetCKSL.ReadOnly = true;
            buttonXG.Enabled = true;
            
            try
            {
                txtBoxCKSL.Text = txtBoxSetCKSL.Text;
                double a, b, c; ;
                a = Convert.ToDouble(txtBoxJHDJ.Text);
                b=Convert.ToDouble(txtBoxCKSL.Text);
               
                int x1, x2, x3,b1;
                x1 = Convert.ToInt32(txtBoxKCSL.Text);
                x2 = Convert.ToInt32(txtBoxCKSL.Text);
                b1 = Convert.ToInt32(txtBoxSetCKSL.Text);
                if (b1 > x1)
                { 
                    MessageBox.Show("出库数量不可大于库存量！");
                    txtBoxSetCKSL.Text = "";
                    txtBoxCKSL.Text = "";
                    txtBoxSetCKSL.ReadOnly = false;
                }
                else
                {
                        c = a * b;
                        txtBoxCKJE.Text = Convert.ToString(c);

                        x3 = x1 - x2;
                        txtBoxSYKC.Text = Convert.ToString(x3);

                        textBox1.Text = txtBoxKPY.Text;
                    
                }
           }
            catch (Exception ex) { MessageBox.Show(ex.Message); return; }
           

           
        }

        private void buttonXG_Click_1(object sender, EventArgs e)
        {
            //txtBoxSetCKSL.ReadOnly  = false;
            labelTX1.Visible = false;
            buttonXG.Enabled = false;
            txtBoxSetCKSL.ReadOnly = false;

           
            
        }

        private void buttonQDCK_Click_1(object sender, EventArgs e)//确定出库
        {
            dataGridView1.Visible=true;
            dataGridView2.Visible=false;
            
            string c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12,c13,c14,c15,c16,c17,c18,c19,c20;
            c2 = "";
            c3 = "";
            c4 = "";
            c5 = "";
            c6 = "";
            c7 = "";
            c8 = "";
            c9 = "";
            c10 = "";
            c11 = "";
            c12 = "";
            c13 = "";
            c14 = "";
            c15 = "";
            c16 = "";
            c17 = "";
            c18 = "";
            c19 = "";
            c20 = "";

            DataTable dt1 = new DataTable();
            dt1 = op.getDataTable("select * from drugsOut", "drugsOut");     //增加一行新数据
          
            c2 = txtBoxCKDH.Text.Trim();
            c3 = txtBoxKPY.Text.Trim();
            c4 = txtBoxYPID.Text.Trim();
            c5 = txtBoxYPMC.Text.Trim();
            c6 = txtBoxPY.Text.Trim();
            c7 = txtBoxGG.Text.Trim();
            c8 = txtBoxPH.Text.Trim();
            c9 = txtBoxKF.Text.Trim();
            c10 = txtBoxCJ.Text.Trim();
            c11 = txtBoxYXQ.Text.Trim();
            c12 = txtBoxUnit.Text.Trim();
            c13 = txtBoxKCSL.Text.Trim();
            c14 = txtBoxJHDJ.Text.Trim();
            c15 = txtBoxXSDJ.Text.Trim();
            c16 = txtBoxCKSL.Text.Trim();
            c17 = txtBoxCKJE.Text.Trim();
            c18 = dateTimePicker1.Value.ToString();
            c19 = txtBoxSYKC.Text.Trim();
            c20 = dateTimePicker1.Text;
            if (textBoxCKID.Text.Trim()=="")
            {
                string sql_new = "insert into drugsOut (drugsOutIdnum,providerName,drugsID,drugsName,drugsPY,drugsSpecification,drugsBatchNo,drugsStorePlace,drugsFactory ,drugsUsefulDate,drugsUnit,drugsCount,drugsInprice,drugsSalePrice,drugsOutNO,drugsOutMoney,drugsOutDate,drugsOutDT)values('"
                            + c2 + "','" + c3 + "','" + c4 + "','" + c5 + "','" + c6 + "','" + c7 + "','" + c8 + "','" + c9 + "','" + c10 + "','" + c11 + "','" + c12 + "','" + c13 + "','" + c14 + "','" + c15 + "','" + c16 + "','" + c17 + "','" + c18 +"','"+c20+ "')"; 
                op.ExecuteSql(sql_new);

                DataTable dt = new DataTable();
                string sql = "select drugsOutIdNum 出库单号,providerName 开票人,drugsID 药品ID,drugsName 药品名称,drugsPY 拼音码,drugsSpecification 规格,drugsBatchNo 批号,drugsStorePlace 库存地址,drugsFactory 生产厂家,drugsUsefulDate 有效期,drugsUnit 药品单位,drugsCount 库存数量,drugsInprice 进货单价,drugsSalePrice 销售单价,drugsOutNO 出库数量,drugsOutMoney 出库金额,drugsOutDate 出库日期 from drugsOut order by drugsOutId";
                dt = op.getDataTable(sql, "drugsOut");
                dataGridView1.DataSource = dt;
                dataGridView1.Rows[0].Selected = false; 
                int index = dt1.Rows.Count;
                this.dataGridView1.Rows[index].Selected = true;

                string select_id = dataGridView1.SelectedRows[0].Cells[2].Value.ToString();//选择的当前行第三列的值，也就是ID
                DataTable dt3 = new DataTable();
                string sql_3 = "update drugsStore set drugsCount=' " + c19 + "' where drugsID='" + select_id + "'";
                dt3 = op.getDataTable(sql_3, "drugsStore");

                //警告提醒
                jgtx();

                MessageBox.Show("出库成功！");
                return;
            }    
        }

    

        private void dataGridView2_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                buttBBDY.Enabled = false;
                buttonBC.Enabled = true;
                buttonXG.Enabled = false;
                buttBBDY.Enabled = true;
                buttonQDCK.Enabled = true;
                txtBoxSetCKSL.Text = "";
                txtBoxCKSL.Text= "";
                txtBoxCKJE.Text = "";

                txtBoxSetCKSL.ReadOnly = false ;
                DataTable dt_out = new DataTable();
                dt_out = op.getDataTable("select * from drugsStore where drugsId='" + dataGridView2.CurrentRow.Cells[0].Value.ToString() + "'", "drugsStore");
                
                this.txtBoxDrugsName.Text = dt_out.Rows[0]["drugsName"].ToString();
                this.txtBoxPY.Text = dt_out.Rows[0]["drugsPY"].ToString();
                this.txtBoxGG.Text = dt_out.Rows[0]["drugsSpecification"].ToString();
                this.txtBoxPH.Text = dt_out.Rows[0]["drugsRegNum"].ToString();
                this.txtBoxCJ.Text = dt_out.Rows[0]["drugsFactory"].ToString();
                this.txtBoxYXQ.Text = dt_out.Rows[0]["drugsUsefulDate"].ToString();
                this.txtBoxKF.Text = dt_out.Rows[0]["drugsStorePlace"].ToString();
                this.txtBoxUnit.Text = dt_out.Rows[0]["drugsUnit"].ToString();
                this.txtBoxKCSL.Text = dt_out.Rows[0]["drugsCount"].ToString();
                this.txtBoxJHDJ.Text = dt_out.Rows[0]["drugsInPrice"].ToString();
                this.txtBoxXSDJ.Text = dt_out.Rows[0]["drugsSalePrice"].ToString();
               
                this.txtBoxYPID.Text = dt_out.Rows[0]["drugsId"].ToString();

                txtBoxYPMC.Text = dt_out.Rows[0]["drugsName"].ToString();

               
                this.txtBoxYPID.Text = dt_out.Rows[0]["drugsID"].ToString();
                this.txtBoxKPYID.Text = u_no;
                //string a=Convert.ToString(dateTimePicker1.Value);
                string str = Convert.ToString(DateTime.Now.Date.Year) + Convert.ToString(DateTime.Now.Date.Month) + Convert.ToString(DateTime.Now.Date.Day);
                txtBoxCKDH.Text = this.txtBoxYPID.Text + this.txtBoxKPYID.Text + str;
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); return; }
        }


        private void timer1_Tick(object sender, EventArgs e)
        {
            this.dateTimePicker1.Text = DateTime.Now.ToString();
            this.dateTimePicker2.Text = DateTime.Now.ToString();
        }

        private void buttBBDY_Click(object sender, EventArgs e)
        {
            labelTX1.Visible = false;
            FrmDrugsOutDetail fdod = new FrmDrugsOutDetail();
            fdod.ShowDialog();
            
        }



        private void buttExit_Click_1(object sender, EventArgs e)
        {
            this.Dispose();
            this.Close();
        }

        private void buttYCKYP_Click(object sender, EventArgs e)
        {
            labelTX1.Visible = false;
            groupBox1.Text = "已出库药品信息";
            buttBBDY.Enabled = true;
            dataGridView1.Visible = true;
            dataGridView2.Visible = false;
            buttonBC.Enabled = false;
            buttonXG.Enabled = false;
            buttonQDCK.Enabled = false;

            txtBoxDrugsName.Text = "";
            txtBoxPY.Text = "";
            txtBoxGG.Text = "";
            txtBoxPH.Text = "";
            txtBoxCJ.Text = "";
            txtBoxYXQ.Text = "";
            txtBoxKF.Text = "";
            txtBoxUnit.Text = "";
            txtBoxKCSL.Text = "";
            txtBoxJHDJ.Text = "";
            txtBoxXSDJ.Text = "";
            txtBoxCKSL.Text = "";
            txtBoxCKJE.Text = "";
            txtBoxSYKC.Text = "";

            DataTable dt = new DataTable();
            string sql = "select drugsOutIdNum 出库单号,providerName 开票人,drugsID 药品ID,drugsName 药品名称,drugsPY 拼音码,drugsSpecification 规格,drugsBatchNo 批号,drugsStorePlace 库存地址,drugsFactory 生产厂家,drugsUsefulDate 有效期,drugsUnit 药品单位,drugsCount 库存数量,drugsInprice 进货单价,drugsSalePrice 销售单价,drugsOutNO 出库数量,drugsOutMoney 出库金额,drugsOutDate 出库日期 from drugsOut order by drugsOutId";
            dt = op.getDataTable(sql, "drugsOut");
            dataGridView1.DataSource = dt;
        }

        private void buttQBYP_Click(object sender, EventArgs e)
        {
            labelTX1.Visible = false;
            buttBBDY.Enabled = false;
            groupBox1.Text = "药品库存信息";
            dataGridView1.Visible = false;
            dataGridView2.Visible = true;
            txtBoxDrugsName.Text = "";
            txtBoxPY.Text = "";
            txtBoxGG.Text = "";
            txtBoxPH.Text = "";
            txtBoxCJ.Text = "";
            txtBoxYXQ.Text = "";
            txtBoxKF.Text = "";
            txtBoxUnit.Text = "";
            txtBoxKCSL.Text = "";
            txtBoxJHDJ.Text = "";
            txtBoxXSDJ.Text = "";
            txtBoxCKSL.Text = "";
            txtBoxCKJE.Text = "";
            txtBoxSYKC.Text = "";

            try
            {
                DataTable dt = new DataTable();
                string sql_a = "select drugsId 药品ID,drugsName 药品名称, drugsPY 拼音码, drugsSpecification 药品规格,drugsUnit 计量单位, drugsCount 库存数量,drugsStorePlace 库存地址,drugsRegNum 批准文号 ,drugsFactory 生产厂家,drugsUsefulDate 有效期,drugsInPrice 进货价格,drugsSalePrice 销售价格 from drugsStore order by Id";
                dt = op.getDataTable(sql_a, "drugsStore");
                dataGridView2.DataSource = dt;
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); return; }
        }

        private void txtBoxPYM_TextChanged_1(object sender, EventArgs e)
        {
            dataGridView2.Visible = true;
            dataGridView1.Visible = false;
            buttBBDY.Enabled = false;
            txtBoxDrugsName.Text = "";
            txtBoxPY.Text = "";
            txtBoxGG.Text = "";
            txtBoxPH.Text = "";
            txtBoxCJ.Text = "";
            txtBoxYXQ.Text = "";
            txtBoxKF.Text = "";
            txtBoxUnit.Text = "";
            txtBoxKCSL.Text = "";
            txtBoxJHDJ.Text = "";
            txtBoxXSDJ.Text = "";
            txtBoxCKSL.Text = "";
            txtBoxCKJE.Text = "";
            txtBoxSYKC.Text = "";

            try
            {
                if (txtBoxPYM.Text != "")
                {
                    DataTable dt = new DataTable();
                    string sql_a = "select drugsId 药品ID,drugsName 药品名称, drugsPY 拼音码, drugsSpecification 药品规格,drugsUnit 计量单位, drugsCount 库存数量,drugsStorePlace 库存地址,drugsRegNum 批准文号 ,drugsFactory 生产厂家,drugsUsefulDate 有效期 from drugsStore where drugsPY like '%" + txtBoxPYM.Text + "%'";
                    dt = op.getDataTable(sql_a, "drugsStore");
                    dataGridView2.DataSource = dt;
                }
                else
                { MessageBox.Show("请输入拼音码！"); }


            }
            catch (Exception ex) { MessageBox.Show(ex.Message); return; }
        }
    }
}
